import 'package:dmz/business/home/controller/ota_record_controller.dart';
import 'package:dmz/utils/Utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../model/moto_record.dart';

class OtaWidget extends StatefulWidget {
  const OtaWidget({
    super.key,
  });

  @override
  State<StatefulWidget> createState() {
    return _OtaWidget();
  }
}

class _OtaWidget extends State<OtaWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OtaRecordController>(builder: (controller) {
      return ListView.builder(
          itemCount: controller.list.length,
          itemBuilder: (context, index) {
            return item(controller.list[index]);
          });
    });
  }

  item(MotoRecord data) {
    String? tempName;
    String otaTime = '升级时间：';
    String? desc;
    String? bug;
    switch (data.status) {
      case 3:
        tempName = '固件升级成功';
        break;
      default:
        tempName = '固件升级失败';
        break;
    }
    otaTime = Utils.timeToDate(data.updated_at!);
    for (int i = 0; i < data.detail!.length; i++) {
      if (i < data.detail!.length - 1) {
        if (Utils.isNotEmptyString(data.detail![i].description)) {
          bug = '$bug ${data.detail![i].description},';
        }
      } else {
        if (Utils.isNotEmptyString(data.detail![i].description)) {
          bug = '$bug ${data.detail![i].description}';
        }
      }
      for (int j = 0; j < data.detail![i].type!.length; j++) {
        if (j < data.detail![i].type!.length - 1) {
          if (Utils.isNotEmptyString(data.detail![i].type![j].name)) {
            desc = '$desc ${data.detail![i].type![j].name},';
          }
        } else {
          if (Utils.isNotEmptyString(data.detail![i].type![j].name)) {
            desc = '$desc ${data.detail![i].type![j].name}';
          }
        }
      }
    }
    return Container(
      padding:
          EdgeInsets.only(top: 36.sp, bottom: 36.sp, left: 30.sp, right: 30.sp),
      color: const Color(0xFF434650),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          const SizedBox(
            height: 20,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(tempName,
                  style:
                      const TextStyle(fontSize: 14, color: Color(0xFFF9F9F9))),
              Text(otaTime,
                  style:
                      const TextStyle(fontSize: 14, color: Color(0xFFF9F9F9))),
            ],
          ),
          const SizedBox(
            height: 20,
          ),
          Text(desc ?? '',
              style: const TextStyle(fontSize: 14, color: Color(0xFFF9F9F9))),
          const SizedBox(
            height: 20,
          ),
          Text(bug ?? '',
              style: const TextStyle(fontSize: 14, color: Color(0xFFF9F9F9))),
          const SizedBox(
            height: 20,
          ),
        ],
      ),
    );
  }
}
